<?php

namespace Admin\Controller;

use Think\Controller;

class AreaController extends BaseController {
    /*
     * 地区列表
     */

    public function area_list() {
        //表单搜索条件
        $name = I('name');
        if (!empty($name)) {
            $map['areaname'] = array('like', '%' . $name . '%');
        }
       
        //保证查询时，分页页码从1开始
        if (IS_POST) {
            $_GET['p'] = 1;
        }
        $count = M('address')->where($map)->count();
        $pagesize = 15;
        $Page = new \Think\Page($count, $pagesize);

        if (!empty($map)) {
            $param = empty($param) ? I('') : array_merge($param, I(''));
            //分页跳转的时候保证查询条件
            if (!empty($param)) {
                foreach ($param as $key => $val) {
                    $Page->parameter[$key] = urlencode($val);
                }
            }
        }
        $res = M('address')
                ->where($map)
                ->limit($Page->firstRow, $Page->listRows)
                ->select();
        $this->assign("data", $res);
        $this->assign("page", $Page->show());
        $this->display();
    }

    /**
     * 添加地区
     */
    public function area_add() {
        if (IS_POST) {
            $areaname = I("areaname"); //名字
            $english = I("english");
          
            if (empty($english) || empty($areaname)) {
                $this->error("参数不完整");
                exit;
            } else {
             
           
                    $data["areaname"] = $areaname;
                    $data["english"] = $english;
                   
                    $re = M("address")->add($data);
                    if ($re) {
                        $this->success("添加成功");
                        exit;
                    } else {
                        $this->error("添加失败");
                        exit;
                    }
                }
            
        }
        $this->display();
    }

    /**
     * 地区编辑
     */
    public function area_edit() {
        if (IS_POST) {
             $areaname = I("areaname"); //名字
            $english = I("english");
            $where["id"] = I("id");
          
            if (empty($english) || empty($areaname) || empty($where["id"])) {
                $this->error("参数不完整");
                exit;
            } else {
             
           
                    $data["areaname"] = $areaname;
                    $data["english"] = $english;
                    
                   
                    $re = M("address")->where($where)->save($data);
                    if ($re) {
                        $this->success("修改成功",U("area_list"));
                        exit;
                    } else {
                        $this->error("内容未做修改");
                        exit;
                    }
                }
        }
        $id = I("id");
        $re = M("address")->where("id=" . $id)->find();
        $this->assign("data", $re);
        $this->display();
    }

    /**
     * 地区删除
     */
    public function area_del() {
        $id = I("id");
        $re = M("address")->where("id=" . $id)->delete();
        if ($re) {
            $this->success("删除成功", U("area/area_list"));
        } else {
            $this->error("删除失败");
        }
    }

}
